package com.wwwarehouse.usercenter.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.business_relationship.BRCustomerFileCheckBean;
import com.wwwarehouse.usercenter.bean.business_relationship.BRCustomerFileListBean;

import java.util.List;

/**
 * Created by xiaoke.ni on 2017/10/13.
 */

public class BRCustomerFileCheckAdapter extends RecyclerView.Adapter<BRCustomerFileCheckAdapter.PRAuthorityAdapterHolder> {

    private final int mRvHeight;
    private BRCustomerFileCheckBean mDataList;
    private Context mContext;

    public interface OnItemClickListener {
        void onButtonClicked(int position);

    }

    private BRCustomerFileCheckAdapter.OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(BRCustomerFileCheckAdapter.OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public BRCustomerFileCheckAdapter(BRCustomerFileCheckBean dataList, int height) {
        this.mDataList = dataList;
        mRvHeight = height;
    }

    @Override
    public BRCustomerFileCheckAdapter.PRAuthorityAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_br_customer_file, parent, false);
        return new BRCustomerFileCheckAdapter.PRAuthorityAdapterHolder(view);
    }

    @Override
    public void onBindViewHolder(final BRCustomerFileCheckAdapter.PRAuthorityAdapterHolder holder, final int position) {
        ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
        params.height = (int) (mRvHeight * 0.85 / 3);
        holder.itemView.setLayoutParams(params);
        BRCustomerFileCheckBean.BusinessListBean userCardAuthoritiesBean = mDataList.getBusinessList().get(position);
        holder.buName.setText(userCardAuthoritiesBean.getViewBuName());
        String tagName = "";
        for (BRCustomerFileCheckBean.BusinessListBean.TagsBean tagsBean : userCardAuthoritiesBean.getTags()){
            if (!"".equals(tagName))
                tagName += "、";
            tagName += tagsBean.getTagName();
        }
        holder.tagName.setText(tagName);
        String salesManName = "";
        for (BRCustomerFileCheckBean.BusinessListBean.SalesMansBean salesMansBean : userCardAuthoritiesBean.getSalesMans()){
            if (!"".equals(salesManName))
                salesManName += "、";
            salesManName += salesMansBean.getSalesManName();
        }
        holder.salesManName.setText(salesManName);
        if (mOnItemClickListener != null) {
            holder.card_rl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onButtonClicked(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.getBusinessList().size();
    }

    static class PRAuthorityAdapterHolder extends RecyclerView.ViewHolder {

        public RelativeLayout card_rl;
        public TextView buName;
        public TextView tagName;
        public TextView salesManName;

        public PRAuthorityAdapterHolder(View itemView) {
            super(itemView);
            card_rl = (RelativeLayout) itemView.findViewById(R.id.card_rl);
            buName = (TextView) itemView.findViewById(R.id.buName);
            tagName = (TextView) itemView.findViewById(R.id.tagName);
            salesManName = (TextView) itemView.findViewById(R.id.salesManName);
        }
    }
}
